package softtech.app.secretcaller;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "myDatabase";

	private final String KEY_PRIMARY = "id";

	final String TAG = "DatabaseHandler";

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sqlCreateTableUser = "create table " + DatabaseUser.TABLE_NAME
				+ "(" + KEY_PRIMARY + " integer primary key,"
				+ DatabaseUser.KEY_ID + " integer," + DatabaseUser.KEY_USERNAME
				+ " text," + DatabaseUser.KEY_PASSWORD + " text,"
				+ DatabaseUser.KEY_EMAIL + " text," + DatabaseUser.KEY_SECRET
				+ " text," + DatabaseUser.KEY_PHONE + " text" + ")";
		db.execSQL(sqlCreateTableUser);

		String sqlCreateTableContact = "create table "
				+ DatabaseContact.TABLE_NAME + "(" + KEY_PRIMARY
				+ " integer primary key," + DatabaseContact.KEY_ID
				+ " integer," + DatabaseContact.KEY_CONTACT_NAME + " text,"
				+ DatabaseContact.KEY_PHONE + " text,"
				+ DatabaseContact.KEY_AVATAR + " text,"
				+ DatabaseContact.KEY_LAST_CALL + " text,"
				+ DatabaseContact.KEY_LAST_SMS + " text,"
				+ DatabaseContact.KEY_ID_USER + " integer,"
				+ DatabaseContact.KEY_IS_SYNC + " integer" + ")";
		db.execSQL(sqlCreateTableContact);

		String sqlCreateTableSms = "create table " + DatabaseSMS.TABLE_NAME
				+ "(" + KEY_PRIMARY + " integer primary key,"
				+ DatabaseSMS.KEY_PHONE + " text," + DatabaseSMS.KEY_CONTENT
				+ " text," + DatabaseSMS.KEY_DATE_TIME + " CURRENT_TIMESTAMP,"
				+ DatabaseSMS.KEY_TYPE + " integer,"
				+ DatabaseSMS.KEY_CONTACT_ID + " integer" + ")";
		db.execSQL(sqlCreateTableSms);

		String sqlCreateTableCall = "create table " + DatabaseCall.TABLE_NAME
				+ "(" + KEY_PRIMARY + " integer primary key,"
				+ DatabaseCall.KEY_PHONE + " text,"
				+ DatabaseCall.KEY_DATE_TIME + " CURRENT_TIMESTAMP,"
				+ DatabaseCall.KEY_TYPE + " integer,"
				+ DatabaseCall.KEY_CONTACT_ID + " integer" + ")";
		db.execSQL(sqlCreateTableCall);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists " + DatabaseUser.TABLE_NAME);
		db.execSQL("drop table if exists " + DatabaseContact.TABLE_NAME);
		db.execSQL("drop table if exists " + DatabaseSMS.TABLE_NAME);

		// Create tables again
		onCreate(db);
	}

	/**
	 * DATABASE USER
	 * 
	 */
	/**
	 * Table user: 1- user_id 2- user_name 3- password 4- email 5- secret key 6-
	 * phone
	 * 
	 */

	// Adding new User
	public long addUser(DatabaseUser user) {
		SQLiteDatabase db = this.getWritableDatabase();
		Log.e(TAG, "USER INFO =" + user.toJSONString());
		// Inserting Row
		long id = db.insert(DatabaseUser.TABLE_NAME, null,
				user.toContentValue());
		db.close(); // Closing database connection
		return id;
	}

	// Getting single User
	public DatabaseUser getUser(int user_id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Log.e(TAG, "Id=" + user_id);

		Cursor cursor = db.query(DatabaseUser.TABLE_NAME, new String[] {
				DatabaseUser.KEY_ID, DatabaseUser.KEY_USERNAME,
				DatabaseUser.KEY_PASSWORD, DatabaseUser.KEY_PHONE,
				DatabaseUser.KEY_SECRET, DatabaseUser.KEY_EMAIL },
				DatabaseUser.KEY_ID + "=?",
				new String[] { String.valueOf(user_id) }, null, null, null,
				null);
		if (cursor != null) {
			cursor.moveToFirst();
		} else {
			Log.e(TAG, "cursor null");
			return null;
		}
		DatabaseUser user = null;
		try {
			user = new DatabaseUser(Integer.parseInt(cursor.getString(0)),
					cursor.getString(1), cursor.getString(4));
			Log.e(TAG, user.toJSONString());
		} catch (Exception e) {

		}
		return user;

	}

	// Get single User
	public DatabaseUser getUser(String username) {
		SQLiteDatabase db = this.getReadableDatabase();
		Log.e(TAG, "Username = " + username);
		Cursor cursor = db.query(DatabaseUser.TABLE_NAME, new String[] {
				DatabaseUser.KEY_ID, DatabaseUser.KEY_USERNAME,
				DatabaseUser.KEY_PASSWORD, DatabaseUser.KEY_PHONE,
				DatabaseUser.KEY_SECRET, DatabaseUser.KEY_EMAIL },
				DatabaseUser.KEY_USERNAME + "=?", new String[] { username },
				null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		} else {
			return null;
		}

		DatabaseUser user = null;
		try {
			user = new DatabaseUser(Integer.parseInt(cursor.getString(0)),
					cursor.getString(1), cursor.getString(4));
			Log.e(TAG, user.toJSONString());
		} catch (Exception e) {

		}

		return user;

	}

	// Getting All User
	public List<DatabaseUser> getAllUser() {
		List<DatabaseUser> userList = new ArrayList<DatabaseUser>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseUser.TABLE_NAME
				+ " ORDER BY " + DatabaseUser.KEY_USERNAME + " ASC";

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				DatabaseUser user = new DatabaseUser(Integer.parseInt(cursor
						.getString(1)), cursor.getString(2),
						cursor.getString(5));
				Log.e(TAG, user.toJSONString());

				userList.add(user);
			} while (cursor.moveToNext());
		}

		// return contact list
		return userList;
	}

	// Getting Users Count
	public int getDatabaseUserCount() {
		String countQuery = "SELECT  * FROM " + DatabaseUser.TABLE_NAME;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();

		// return count
		return cursor.getCount();
	}

	// Updating single User
	public int updateUser(DatabaseUser user) {
		Log.e(TAG, "UPDATE USER " + user.toJSONString());
		SQLiteDatabase db = this.getWritableDatabase();

		return db.update(DatabaseUser.TABLE_NAME, user.toContentValue(),
				DatabaseUser.KEY_ID + " = ?",
				new String[] { String.valueOf(user.get_Id()) });
	}

	// Deleting single User
	public void deleteUser(DatabaseUser user) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(DatabaseUser.TABLE_NAME, DatabaseUser.KEY_ID + " = ?",
				new String[] { String.valueOf(user.get_Id()) });
		db.close();
	}

	/**
	 * DATABASE CONTACT
	 * 
	 */
	/**
	 * 1- id 2- name 3- phone 4- avatar 5- last call 6- last sms 7- id user 8-
	 * is sync
	 */

	// Adding new contact
	public long addContact(DatabaseContact contact) {
		if (contact == null) {
			return -1;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		// Inserting Row
		Log.e(TAG, " contact info = " + contact.toContentValue());
		long id = db.insert(DatabaseContact.TABLE_NAME, null,
				contact.toContentValue());
		db.close(); // Closing database connection
		return id;
	}

	// Getting single contact
	public DatabaseContact getContact(int contact_id, int user_id) {
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id
				+ " AND " + DatabaseContact.KEY_ID + "=" + contact_id;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor != null) {
			cursor.moveToFirst();
		} else {
			Log.e(TAG, "cursor null");
			return null;
		}
		if (cursor.getCount() == 0) {
			return null;
		}
		int _id = Integer.parseInt(cursor.getString(1));
		String _name = cursor.getString(2);
		String _phone = cursor.getString(3);
		String _avatar = cursor.getString(4);
		String _lastcall = cursor.getString(5);
		String _last_sms = cursor.getString(6);
		int _id_user = cursor.getInt(7);
		int _is_sync = cursor.getInt(8);

		DatabaseContact contact = new DatabaseContact(_id, _name, _phone,
				_id_user);
		contact.setPrimaryKey(cursor.getInt(0));
		contact.setIsSync(_is_sync);
		if (ToolsSaveFile.checkStringNotNull(_avatar)) {
			contact.setAvatar(_avatar);
		}

		if (ToolsSaveFile.checkStringNotNull(_lastcall)) {
			contact.setLastCall(_lastcall);
		}

		if (ToolsSaveFile.checkStringNotNull(_last_sms)) {
			contact.setLastSms(_last_sms);
		}

		return contact;
	}

	// Getting single contact
	public DatabaseContact getContact(String contact_name, int user_id) {
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id
				+ " AND " + DatabaseContact.KEY_CONTACT_NAME + "='"
				+ contact_name + "'";

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor != null) {
			cursor.moveToFirst();
		} else {
			Log.e(TAG, "cursor null");
			return null;
		}

		int _id = Integer.parseInt(cursor.getString(1));
		String _name = cursor.getString(2);
		String _phone = cursor.getString(3);
		String _avatar = cursor.getString(4);
		String _lastcall = cursor.getString(5);
		String _last_sms = cursor.getString(6);
		int _id_user = cursor.getInt(7);
		int _is_sync = cursor.getInt(8);

		DatabaseContact contact = new DatabaseContact(_id, _name, _phone,
				_id_user);
		contact.setPrimaryKey(cursor.getInt(0));
		contact.setIsSync(_is_sync);
		if (ToolsSaveFile.checkStringNotNull(_avatar)) {
			contact.setAvatar(_avatar);
		}

		if (ToolsSaveFile.checkStringNotNull(_lastcall)) {
			contact.setLastCall(_lastcall);
		}

		if (ToolsSaveFile.checkStringNotNull(_last_sms)) {
			contact.setLastSms(_last_sms);
		}

		return contact;
	}

	// Getting single contact
	public DatabaseContact getContactFromPhoneNumber(String phone_number,
			int user_id) {
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id
				+ " AND " + DatabaseContact.KEY_PHONE + " = '" + phone_number
				+ "'";

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
		} else {
			Log.e(TAG, "cursor null");
			return null;
		}

		int _id = Integer.parseInt(cursor.getString(1));
		String _name = cursor.getString(2);
		String _phone = cursor.getString(3);
		int _id_user = cursor.getInt(7);

		DatabaseContact contact = new DatabaseContact(_id, _name, _phone,
				_id_user);
		contact.setPrimaryKey(cursor.getInt(0));

		return contact;
	}

	// Getting single contact
	public DatabaseContact getContactLikePhoneNumber(String phone_number,
			int user_id) {
		// Select All Query
		if(phone_number.length()>4){
			phone_number = phone_number.substring(3);
		}
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id
				+ " AND " + DatabaseContact.KEY_PHONE + " LIKE '%"
				+ phone_number + "%'";
		Log.e(TAG, selectQuery);
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
		} else {
			Log.e(TAG, "cursor null");
			return null;
		}

		int _id = Integer.parseInt(cursor.getString(1));
		String _name = cursor.getString(2);
		String _phone = cursor.getString(3);
		int _id_user = cursor.getInt(7);

		DatabaseContact contact = new DatabaseContact(_id, _name, _phone,
				_id_user);
		contact.setPrimaryKey(cursor.getInt(0));

		return contact;
	}

	// Getting single contact
	public long getContactIdLikePhoneNumber(String phone_number,
			int user_id) {
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id
				+ " AND " + DatabaseContact.KEY_PHONE + " LIKE '%"
				+ phone_number + "%'";
		Log.e(TAG, selectQuery);
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
		} else {
			Log.e(TAG, "cursor null");
			return -1;
		}

		int _id = Integer.parseInt(cursor.getString(1));
		String _name = cursor.getString(2);
		String _phone = cursor.getString(3);
		int _id_user = cursor.getInt(7);

		DatabaseContact contact = new DatabaseContact(_id, _name, _phone,
				_id_user);
		contact.setPrimaryKey(cursor.getInt(0));

		return contact.get_Id();
	}
	
	// Getting All Contacts
	public ArrayList<DatabaseContact> getAllContacts(int user_id) {
		ArrayList<DatabaseContact> contactList = new ArrayList<DatabaseContact>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id
				+ " ORDER BY " + DatabaseContact.KEY_CONTACT_NAME + " ASC";
		Log.e(TAG, selectQuery);
		SQLiteDatabase db = this.getWritableDatabase();
		try {
			Cursor cursor = db.rawQuery(selectQuery, null);

			// looping through all rows and adding to list
			if (cursor.moveToFirst()) {
				do {
					int _id = Integer.parseInt(cursor.getString(1));
					String _name = cursor.getString(2);
					String _phone = cursor.getString(3);
					String _avatar = cursor.getString(4);
					String _lastcall = cursor.getString(5);
					String _last_sms = cursor.getString(6);
					int _id_user = cursor.getInt(7);
					int _is_sync = cursor.getInt(8);

					DatabaseContact contact = new DatabaseContact(_id, _name,
							_phone, _id_user);
					contact.setPrimaryKey(cursor.getInt(0));
					contact.setIsSync(_is_sync);
					if (ToolsSaveFile.checkStringNotNull(_avatar)) {
						contact.setAvatar(_avatar);
					}

					if (ToolsSaveFile.checkStringNotNull(_lastcall)) {
						contact.setLastCall(_lastcall);
					}

					if (ToolsSaveFile.checkStringNotNull(_last_sms)) {
						contact.setLastSms(_last_sms);
					}
					contactList.add(contact);
				} while (cursor.moveToNext());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		// return contact list
		return contactList;
	}

	// Getting All Contacts ORDER BY field
	public ArrayList<DatabaseContact> getAllContacts(int user_id,
			String order_by_field) {
		ArrayList<DatabaseContact> contactList = new ArrayList<DatabaseContact>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id;
		if (ToolsSaveFile.checkStringNotNull(order_by_field)) {
			String ORDER_BY_CAUSE = " ORDER BY " + order_by_field + " ASC";
			selectQuery = selectQuery + ORDER_BY_CAUSE;
		}

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				int _id = Integer.parseInt(cursor.getString(1));
				String _name = cursor.getString(2);
				String _phone = cursor.getString(3);
				String _avatar = cursor.getString(4);
				String _lastcall = cursor.getString(5);
				String _last_sms = cursor.getString(6);
				int _id_user = cursor.getInt(7);
				int _is_sync = cursor.getInt(8);

				DatabaseContact contact = new DatabaseContact(_id, _name,
						_phone, _id_user);
				contact.setPrimaryKey(cursor.getInt(0));
				contact.setIsSync(_is_sync);
				if (ToolsSaveFile.checkStringNotNull(_avatar)) {
					contact.setAvatar(_avatar);
				}

				if (ToolsSaveFile.checkStringNotNull(_lastcall)) {
					contact.setLastCall(_lastcall);
				}

				if (ToolsSaveFile.checkStringNotNull(_last_sms)) {
					contact.setLastSms(_last_sms);
				}
				contactList.add(contact);
			} while (cursor.moveToNext());
		}

		// return contact list
		return contactList;
	}

	// Getting contacts Count
	public int getDatabaseContactCount(int user_id) {
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseContact.TABLE_NAME
				+ " WHERE " + DatabaseContact.KEY_ID_USER + "=" + user_id;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		cursor.close();

		// return count
		return cursor.getCount();
	}

	// Updating single contact
	public int updateContact(DatabaseContact contact) {
		SQLiteDatabase db = this.getWritableDatabase();

		String[] args = new String[] { Integer.toString(contact.get_Id()),
				Integer.toString(contact.get_User_Id()) };

		return db.update(DatabaseContact.TABLE_NAME, contact.toContentValue(),
				DatabaseContact.KEY_ID + " = ? AND "
						+ DatabaseContact.KEY_ID_USER + " = ? ", args);
	}

	// Updating single contact
	public int updateContact(long primary_id, DatabaseContact contact) {
		SQLiteDatabase db = this.getWritableDatabase();

		return db.update(DatabaseContact.TABLE_NAME, contact.toContentValue(),
				KEY_PRIMARY + " = " + primary_id, null);
	}

	// Update single contact lastcall or last sms
	public int updateContactLastCall(long primary_id, DatabaseContact contact) {
		Log.d(TAG, "UPDATE CONTACT");
		SQLiteDatabase db = this.getWritableDatabase();

		return db.update(DatabaseContact.TABLE_NAME,
				contact.toLastCallContentValue(), KEY_PRIMARY + " = "
						+ primary_id, null);
	}

	// Update single contact lastcall or last sms
	public int updateContactLastSms(long primary_id, DatabaseContact contact) {
		Log.d(TAG, "UPDATE CONTACT");
		SQLiteDatabase db = this.getWritableDatabase();

		return db.update(DatabaseContact.TABLE_NAME,
				contact.toLastCallContentValue(), KEY_PRIMARY + " = "
						+ primary_id, null);
	}

	// Deleting single contact
	public void deleteContact(DatabaseContact contact) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] args = new String[] { Integer.toString(contact.get_Id()),
				Integer.toString(contact.get_User_Id()) };
		db.delete(DatabaseContact.TABLE_NAME, DatabaseContact.KEY_ID
				+ " = ? AND " + DatabaseContact.KEY_ID_USER + " = ? ", args);
		db.close();
	}

	// Deleting single contact
	public void deleteContact(long primary_key) {
		SQLiteDatabase db = this.getWritableDatabase();

		db.delete(DatabaseContact.TABLE_NAME,
				KEY_PRIMARY + " = " + primary_key, null);
		db.close();
	}

	/**
	 * DATABASE SMS
	 */
	/**
	 * 0- primary 1- phone 2- content 3- date time 4- type 5- contact_id
	 */

	// ADD a sms to database
	public long addNewSMS(DatabaseSMS sms) {
		if (sms == null) {
			return -1;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		// Inserting Row
		Log.e(TAG, " contact info = " + sms.toContentValues());
		long id = db
				.insert(DatabaseSMS.TABLE_NAME, null, sms.toContentValues());
		db.close(); // Closing database connection
		return id;
	}

	/**
	 * get list sms from contact id
	 * 
	 * @param contact_id
	 * @return list contact id
	 */
	public ArrayList<DatabaseSMS> getListSMS(long contact_id) {
		ArrayList<DatabaseSMS> list = new ArrayList<DatabaseSMS>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseSMS.TABLE_NAME
				+ " WHERE " + DatabaseSMS.KEY_CONTACT_ID + "=" + contact_id
				+ " ORDER BY " + DatabaseSMS.KEY_DATE_TIME + " ASC";
		Log.e(TAG, selectQuery);
		SQLiteDatabase db = this.getWritableDatabase();
		try {
			Cursor cursor = db.rawQuery(selectQuery, null);

			// looping through all rows and adding to list
			if (cursor.moveToFirst()) {
				do {
					int _id = cursor.getInt(0);
					String phone = cursor.getString(1);
					String content = cursor.getString(2);
					String date_time = cursor.getString(3);
					int type = cursor.getInt(4);
					int con_id = cursor.getInt(5);

					DatabaseSMS sms_temp = new DatabaseSMS(_id, con_id, phone,
							content, date_time, type);

					list.add(sms_temp);

				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 
	 * @return
	 */
	public DatabaseSMS getSMS(int id) {
		DatabaseSMS sms_item = null;
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseSMS.TABLE_NAME
				+ " WHERE " + KEY_PRIMARY + "=" + id + " ORDER BY "
				+ DatabaseSMS.KEY_DATE_TIME + " ASC";
		Log.e(TAG, selectQuery);

		return sms_item;
	}

	/**
	 * Database Call
	 */
	/**
	 * 0- primary 1- phone 2- date time 3- type 4- contact_id
	 */

	// ADD a Call to database
	public long addNewCall(DatabaseCall Call) {
		if (Call == null) {
			return -1;
		}

		try {
			if (!ToolsSaveFile.checkStringNotNull(Call.getPhone())) {
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		SQLiteDatabase db = this.getWritableDatabase();
		// check data already exist
		String subString = Call.getDate().substring(0, 16);
		String selectQuery = "SELECT  * FROM " + DatabaseCall.TABLE_NAME
				+ " WHERE " + DatabaseCall.KEY_DATE_TIME + " LIKE '%"
				+ subString + "%'";

		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor != null && cursor.getCount() > 0) {
			return -1;
		}

		// Inserting Row
		Log.e(TAG, " contact info = " + Call.toContentValues());
		long id = db.insert(DatabaseCall.TABLE_NAME, null,
				Call.toContentValues());
		db.close(); // Closing database connection
		return id;
	}

	/**
	 * get list Call from contact id
	 * 
	 * @param contact_id
	 * @return list contact id
	 */
	public ArrayList<DatabaseCall> getListCall() {
		ArrayList<DatabaseCall> list = new ArrayList<DatabaseCall>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseCall.TABLE_NAME
				+ " ORDER BY " + DatabaseCall.KEY_DATE_TIME + " DESC";
		Log.e(TAG, selectQuery);
		SQLiteDatabase db = this.getWritableDatabase();
		try {
			Cursor cursor = db.rawQuery(selectQuery, null);

			// looping through all rows and adding to list
			if (cursor.moveToFirst()) {
				do {
					int _id = cursor.getInt(0);
					String phone = cursor.getString(1);
					String date_time = cursor.getString(2);
					int type = cursor.getInt(3);
					int con_id = cursor.getInt(4);

					DatabaseCall Call_temp = new DatabaseCall(_id, con_id,
							phone, date_time, type);

					list.add(Call_temp);

				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 
	 * @return item call of id
	 */
	public DatabaseCall getCall(int id) {
		DatabaseCall Call_item = null;
		// Select All Query
		String selectQuery = "SELECT  * FROM " + DatabaseCall.TABLE_NAME
				+ " WHERE " + KEY_PRIMARY + "=" + id + " ORDER BY "
				+ DatabaseCall.KEY_DATE_TIME + " ASC";
		Log.e(TAG, selectQuery);

		return Call_item;
	}

	/**
	 * Delete a caller
	 * 
	 * @param caller
	 */
	public void deleteCaller(DatabaseCall caller) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(DatabaseUser.TABLE_NAME, DatabaseCall.KEY_PRIMARY + " = ?",
				new String[] { String.valueOf(caller.getId()) });
		db.close();
	}
	
	// Updating single Caller
	public int updateCaller(DatabaseCall caller) {
		
		SQLiteDatabase db = this.getWritableDatabase();

		return db.update(DatabaseCall.TABLE_NAME, caller.toContentValues(),
				DatabaseCall.KEY_PRIMARY + " = ?",
				new String[] { String.valueOf(caller.getId()) });
	}
}
